<?php
namespace data\sys\power;
use \data\BaseData;
/**
 * 角色数据对象
 */
class RoleData extends BaseData{
    /**
     * 读取一个角色信息
     */
    public function find($param){
        $fields = ['id','plat','status'];
        $where = $this->parseWhere($fields,$param);
        $info  = M('sys_power_role')->where($where)->find();
        return $info;
    }
    
    /**
     * 添加一个角色信息
     */
    public function add($data){
        $check = [
            'plat'      => ['check'=>'must','msg'=> '角色所属平台不能为空','reg'=>'int'],
            'role'      => ['check'=>'must','msg'=>'请填写角色名称'],
            'remark'    => '',
            'status'    => ['def'=>1],
            'type'      => ['def'=>2,'reg'=> [1,2]]
        ];        
        $save = $this->verifyData($check,$data);  
        return M('sys_power_role')->insertGetId($save);
    }
    
    /**
     * 修改一个角色信息
     */
    public function edit($id,$data){
        $check = [
            'plat'      => ['reg'=>'int'],
            'role'      => ['check'=>'must','msg'=>'请填写角色名称'],
            'remark'    => '',
            'status'    => '',
            'type'      => ['reg'=> [1,2]]
        ];        
        $save = $this->verifyData($check,$data);
        $where = ['id'=>$id];
        $result = M('sys_power_role')->where('id',$id)->update($save);
        if( $result === false ) dataE('更新角色失败');
        return true;
    }
    
    /**
     * 列表读取
     */
    public function getList($param){
        $page = $param['page']?:1;
        $limit= $param['limit']?:20;
        $check = ['plat','status'=>1,'role'=> 'like'];
        $where = $this->parseWhere($check,$param);
        $list =  M('sys_power_role')->where($where)->page($page)->limit($limit)->order('id desc')->select();
        $total = M('sys_power_role')->where($where)->count();
        return [            
            'page' => $page,
            'limit' => $limit,
            'total'=> $total,
            'list' => $list,
        ];
    }
    
    
    
}